package net.chathu.airgw.packets.gw.serverpackets 
{
	import net.chathu.airgw.Config;
	import net.chathu.airgw.Debug;
	import net.chathu.airgw.model.ChatData;
	import net.chathu.airgw.model.JoinedRoomInfo;
	import net.chathu.airgw.model.Room;
	import net.chathu.airgw.model.SessionData;
	import net.chathu.airgw.model.User;
	import net.chathu.airgw.PrefixManager;
	/**
	 * ...
	 * @author ze
	 */
	public class NamesPacket implements ISendableIRCPacket 
	{
		private static const _head:String = " 353 ";
		private var _nick:String;
		private var _joinedRoomInfo:JoinedRoomInfo;
		private var _sessionData:SessionData;
		private var _channelName:String;
		
		public function NamesPacket(nick:String, joinedRoomInfo:JoinedRoomInfo, channelName:String, sessionData:SessionData) 
		{
			this._nick = nick;
			this._joinedRoomInfo = joinedRoomInfo;
			this._channelName = channelName;
			this._sessionData = sessionData;
		}
		
		public function getMessage():String
		{
			var ret:String = "";
			
			var currNames:String = "";
			for (var userId:* in _joinedRoomInfo.userlistWithRights)
			{
				userId = parseInt(userId);
				var user:User = _sessionData.chatData.users.getUser(userId) as User;
				var prefix:String = PrefixManager.getPrefix(_sessionData, user, _joinedRoomInfo);
				
				currNames +=  prefix +  user.name + " ";
				/*if (prefix != "")
				{
					Debug.echo("Prefix nem ures: " + prefix +  user.name); 
				}*/
				
				//TODO: ez csak egy tetszoleges szam, a pontos/jobb ertekkel majd javitani kell!
				if (currNames.length >= 160) 
				{
					ret += ":" + Config.IRC_HOST + _head + _nick + " = " + _channelName + " :" + currNames +"\n";
					currNames = "";
				}
			}
			
			if (currNames.length > 0)
			{
				ret += ":" + Config.IRC_HOST + _head + _nick + " = " + _channelName + " :" + currNames +"\n";
			}
			
			
			return ret;
		}
		
	}

}